package com.b2b.controller.company;


import com.b2b.model.entity.company.CompanyInfo;
import com.b2b.service.WalletService;
import com.b2b.util.ShopUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.CompanyInfoDto;
import com.b2b.model.WalletDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.CompanyInfoService;
import com.b2b.util.CompanyInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/companyInfo")
@PropertySource("classpath:config.properties")
public class CompanyInfoController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final CompanyInfoUtil companyInfoUtil;

    private final CompanyInfoService companyInfoService;

    private final WalletService walletService;

    private final ShopUtil shopUtil;
    @Autowired
    public CompanyInfoController(CompanyInfoUtil companyInfoUtil, CompanyInfoService companyInfoService,
                                 WalletService walletService, ShopUtil shopUtil){
        this.companyInfoUtil = companyInfoUtil;
        this.companyInfoService = companyInfoService;
        this.walletService = walletService;
        this.shopUtil = shopUtil;
    }

    /**
     * 获取公司信息列表列表
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:41
     */
    @GetMapping("/getAll")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean CompanyInfo(@Validated BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<CompanyInfo> companyInfos = companyInfoService.selectAll();
        PageInfo<CompanyInfo> selectPage = new PageInfo<CompanyInfo>(companyInfos);
        if (companyInfos == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }
    /**
     * 获取指定企业信息
     * @param idList
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @GetMapping("/getDetail")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findById(@RequestParam Map<String,Object> idList) {
        if (idList.containsKey("companyId")) {
            Integer companyId = Integer.parseInt(idList.get("companyId").toString());
            CompanyInfo companyInfo = new CompanyInfo();
            companyInfo.setCompanyId(companyId);
            int count = companyInfoService.selectCount(companyInfo);
            if (count <= 0) {
                throw new CustomException("查询失败(Query Failure)");
            }
            CompanyInfoDto companyInfoDto = companyInfoUtil.getById(companyId);
            return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", companyInfoDto);
        }else {
            Integer shopId = Integer.parseInt(idList.get("shopId").toString());
            Integer companyId = shopUtil.getCompanyId(shopId);
            CompanyInfo companyInfo = new CompanyInfo();
            companyInfo.setCompanyId(companyId);
            int count = companyInfoService.selectCount(companyInfo);
            if (count <= 0){
                throw new CustomException("查询失败(Query Failure)");
            }
            CompanyInfoDto companyInfoDto = companyInfoUtil.getById(companyId);
            return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", companyInfoDto);
        }
    }

    /**
     * 删除相关企业信息
     * @param companyInfoDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:43
     */
    @DeleteMapping("/delete")
    // @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestBody CompanyInfoDto companyInfoDto) {
        Integer id = companyInfoDto.getCompanyId();
        int count = companyInfoService.deleteByPrimaryKey(id);
        if (count <= 0) {
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }



    /**
     * 更新企业路由
     * @param companyInfoDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PutMapping("/update")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody CompanyInfoDto companyInfoDto) {
        // 查询数据库密码
        Integer companyId = companyInfoDto.getCompanyId();
        CompanyInfo companyInfo = new CompanyInfo();
        companyInfo.setCompanyId(companyId);
        Integer count = companyInfoService.selectCount(companyInfo);
        if (count <= 0){
            throw new CustomException("查询失败(Query Failure)");
        }
        count = companyInfoUtil.updateInfo(companyInfoDto);
        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", null);
    }




}
